<?php
/**
 * Created by PhpStorm.
 * User: cxj
 * Date: 16/12/28
 * Time: 下午1:05
 */
use app\assets\AppAsset;
$baseUrl = Yii::$app->request->baseUrl;
AppAsset::addCss($this, $baseUrl . "/css/publish.css");
AppAsset::addCss($this, $baseUrl . "/css/home.css");
AppAsset::addScript($this, $baseUrl . "/js/bootstrap3-validation.js");

?>
<link href="../../umeditor/themes/default/css/umeditor.css" type="text/css" rel="stylesheet">
<script type="text/javascript" charset="utf-8" src="../../umeditor/umeditor.config.js"></script>
<script type="text/javascript" charset="utf-8" src="../../umeditor/umeditor.min.js"></script>
<script type="text/javascript" src="../../umeditor/lang/zh-cn/zh-cn.js"></script>

<form name="form" id="form" hidden>
    <input type="file" name="img" class="upload" id="upload" multiple="multiple">
</form>
<div class="form">
    <form>
        <div class="inline" style="height: 190px">
            <label class="form-label" style="line-height: normal">活动封面:</label>


                <img src="" id="activity_poster" style="width: 280px;height: 190px;background-color: #283041;float: left">

            <div class="poster-select">
                <input type="button" class="btn btn-default my-btn" id="fileBtn"
                       style="margin-left: 25px;margin-top: 90px" value="上传图片">
                <p class="poster-tips">注：海报图片不能超过5M，并且不能小于750*558</p>
            </div>

        </div>
        <div class="inline">
            <label class="form-label">活动标题:</label>
            <input class="input-control" type="text" placeholder="20字以内" id="title">
        </div>
        <div class="inline">
            <label class="form-label">活动城市:</label>
            <input class="input-control input-control-city" type="text" placeholder="城市" id="city">
            <input class="input-control input-address" type="text" placeholder="地址" id="address">
        </div>
        <div class="inline">
            <label class="form-label">活动时间:</label>
            <div class="time">
                <!--                <input class="input-control input-time" placeholder="点击选择开始时间">-->
                <!--                <div class="daterange daterange--single"></div>-->
                <input type="date" placeholder="选择时间" class="date" id="start_date">
                <select class="form-control select-time" id="start_hour">
                    <option value="00:00">00:00</option>
                    <option value="00:30">00:30</option>
                    <option value="01:00">01:00</option>
                    <option value="01:30">01:30</option>
                    <option value="02:00">02:00</option>
                    <option value="02:30">02:30</option>
                    <option value="03:00">03:00</option>
                    <option value="03:30">03:30</option>
                    <option value="04:00">04:00</option>
                    <option value="04:30">04:30</option>
                    <option value="05:00">05:00</option>
                    <option value="05:30">05:30</option>
                    <option value="06:00">06:00</option>
                    <option value="06:30">06:30</option>
                    <option value="07:00">07:00</option>
                    <option value="07:30">07:30</option>
                    <option value="08:00">08:00</option>
                    <option value="08:30">08:30</option>
                    <option value="09:00">09:00</option>
                    <option value="09:30">09:30</option>
                    <option value="10:00">10:00</option>
                    <option value="10:30">10:30</option>
                    <option value="11:00">11:00</option>
                    <option value="11:30">11:30</option>
                    <option value="12:00">12:00</option>
                    <option value="12:30">12:30</option>
                    <option value="13:00" selected>13:00</option>
                    <option value="13:30">13:30</option>
                    <option value="14:00">14:00</option>
                    <option value="14:30">14:30</option>
                    <option value="15:00">15:00</option>
                    <option value="15:30">15:30</option>
                    <option value="16:00">16:00</option>
                    <option value="16:30">16:30</option>
                    <option value="17:00">17:00</option>
                    <option value="17:30">17:30</option>
                    <option value="18:00">18:00</option>
                    <option value="18:30">18:30</option>
                    <option value="19:00">19:00</option>
                    <option value="19:30">19:30</option>
                    <option value="20:00">20:00</option>
                    <option value="20:30">20:30</option>
                    <option value="21:00">21:00</option>
                    <option value="21:30">21:30</option>
                    <option value="22:00">22:00</option>
                    <option value="22:30">22:30</option>
                    <option value="23:00">23:00</option>
                    <option value="23:30">23:30</option>
                </select aside>
            </div>
            <div class="time">~</div>
            <div class="time">
                <!--                <div class="daterange daterange--single"></div>-->
                <input type="date" placeholder="选择时间" class="date" id="over_date">
                <select id="over_hour" class="form-control select-time">
                    <option value="00:00">00:00</option>
                    <option value="00:30">00:30</option>
                    <option value="01:00">01:00</option>
                    <option value="01:30">01:30</option>
                    <option value="02:00">02:00</option>
                    <option value="02:30">02:30</option>
                    <option value="03:00">03:00</option>
                    <option value="03:30">03:30</option>
                    <option value="04:00">04:00</option>
                    <option value="04:30">04:30</option>
                    <option value="05:00">05:00</option>
                    <option value="05:30">05:30</option>
                    <option value="06:00">06:00</option>
                    <option value="06:30">06:30</option>
                    <option value="07:00">07:00</option>
                    <option value="07:30">07:30</option>
                    <option value="08:00">08:00</option>
                    <option value="08:30">08:30</option>
                    <option value="09:00">09:00</option>
                    <option value="09:30">09:30</option>
                    <option value="10:00">10:00</option>
                    <option value="10:30">10:30</option>
                    <option value="11:00">11:00</option>
                    <option value="11:30">11:30</option>
                    <option value="12:00">12:00</option>
                    <option value="12:30">12:30</option>
                    <option value="13:00" selected>13:00</option>
                    <option value="13:30">13:30</option>
                    <option value="14:00">14:00</option>
                    <option value="14:30">14:30</option>
                    <option value="15:00">15:00</option>
                    <option value="15:30">15:30</option>
                    <option value="16:00">16:00</option>
                    <option value="16:30">16:30</option>
                    <option value="17:00">17:00</option>
                    <option value="17:30">17:30</option>
                    <option value="18:00">18:00</option>
                    <option value="18:30">18:30</option>
                    <option value="19:00">19:00</option>
                    <option value="19:30">19:30</option>
                    <option value="20:00">20:00</option>
                    <option value="20:30">20:30</option>
                    <option value="21:00">21:00</option>
                    <option value="21:30">21:30</option>
                    <option value="22:00">22:00</option>
                    <option value="22:30">22:30</option>
                    <option value="23:00">23:00</option>
                    <option value="23:30">23:30</option>
                </select>
            </div>
        </div>
        <div class="inline">
            <label class="form-label">报名截止时间:</label>
            <input type="date" placeholder="选择时间" class="date" id="deadline">
            <select class="form-control select-time" id="deadHour" >
                <option value="00:00">00:00</option>
                <option value="00:30">00:30</option>
                <option value="01:00">01:00</option>
                <option value="01:30">01:30</option>
                <option value="02:00">02:00</option>
                <option value="02:30">02:30</option>
                <option value="03:00">03:00</option>
                <option value="03:30">03:30</option>
                <option value="04:00">04:00</option>
                <option value="04:30">04:30</option>
                <option value="05:00">05:00</option>
                <option value="05:30">05:30</option>
                <option value="06:00">06:00</option>
                <option value="06:30">06:30</option>
                <option value="07:00">07:00</option>
                <option value="07:30">07:30</option>
                <option value="08:00">08:00</option>
                <option value="08:30">08:30</option>
                <option value="09:00">09:00</option>
                <option value="09:30">09:30</option>
                <option value="10:00">10:00</option>
                <option value="10:30">10:30</option>
                <option value="11:00">11:00</option>
                <option value="11:30">11:30</option>
                <option value="12:00">12:00</option>
                <option value="12:30">12:30</option>
                <option value="13:00" selected>13:00</option>
                <option value="13:30">13:30</option>
                <option value="14:00">14:00</option>
                <option value="14:30">14:30</option>
                <option value="15:00">15:00</option>
                <option value="15:30">15:30</option>
                <option value="16:00">16:00</option>
                <option value="16:30">16:30</option>
                <option value="17:00">17:00</option>
                <option value="17:30">17:30</option>
                <option value="18:00">18:00</option>
                <option value="18:30">18:30</option>
                <option value="19:00">19:00</option>
                <option value="19:30">19:30</option>
                <option value="20:00">20:00</option>
                <option value="20:30">20:30</option>
                <option value="21:00">21:00</option>
                <option value="21:30">21:30</option>
                <option value="22:00">22:00</option>
                <option value="22:30">22:30</option>
                <option value="23:00">23:00</option>
                <option value="23:30">23:30</option>
            </select aside>
        </div>
        <div class="inline">
            <label class="form-label">主办方:</label>
            <input class="input-control" style="width: 300px" type="text" id="sponsor">
        </div>
        <div class="inline">
            <label class="form-label">活动详情:</label>
            <script type="text/plain" id="myEditor" style="width:600px;height:240px;"></script>
        </div>
        <div class="inline">
            <label class="form-label">所属项目类别:</label>
            <select class="form-control select-project" id="project">
                <option value="大学生创业教育项目">大学生创业教育项目</option>
                <option value="专技人才创业项目">专技人才创业项目</option>
                <option value="百万家企业转型项目">百万家企业转型项目</option>
                <option value="女性创业能力开发项目">女性创业能力开发项目</option>
                <option value="跨境电商创业项目">跨境电商创业项目</option>
                <option value="股权设计项目">股权设计项目</option>

            </select>
            <input type="checkbox" value="1" id="show" style="display: inline-block;margin-left: 200px">显示
            <input type="checkbox" value="1" id="stick" style="display: inline-block;margin-left: 10px">置顶
        </div>
        <div class="inline">
            <label class="form-label">门票:</label>
            <div class="ticket">

                <div class="inline">
                    <label class="form-label">票的类型:</label>
                    <select class="form-control select-project" id="ticketType">
                        <option value="普通票" selected>普通票</option>
                        <option value="预购票">预购票</option>
                    </select>
                </div>
                <div class="inline">
                    <label class="form-label">单价:</label>
                    <input type="text" id="price" placeholder="元/张" class="input-control" style="width: 300px">
                </div>
                <div class="inline">
                    <label class="form-label">数量:</label>
                    <input type="text" id="num" class="input-control" style="width: 300px" placeholder="张">
                </div>

            </div>
        </div>
        <div class="operation">
            <button class=" btn btn-default publish-btn" id="publish">发布</button>
            <button class=" btn btn-default" id="draft" style="margin-left: 15px;">存为草稿</button>
            <button class=" btn btn-default " style="margin-left: 15px;">预览</button>
        </div>
    </form>
</div>

<script>

    //初始化编辑器
    var um = UM.getEditor('myEditor');

    //获取输入内容
    function getContent() {
        return UM.getEditor('myEditor').getContent();
    }
    //点击图片来选择上传的图片
    $("#fileBtn").on('click',function(){
        $("#upload").click();
    });

    $("#upload").change(function(d) {
        var formdata = new FormData($("#form")[0]);
        formdata.append("type", "0");
        formdata.append("names", "img");
        var file = this.files[0];
        var file_name = file.name;
        var type = file_name.split(".")[1];
        console.log('formdata',formdata);
        if (type == "jpg" || type == "png" || type == "jpeg") {
            $.ajax({
                type: 'POST',
                url: './uploader',
                data: formdata,
                cache: false,
                processData: false,
                contentType: false,
                mimeType: "multipart/form-data",
                success: function (data) {
                    if (data) {
                        $('#activity_poster').attr("src", "" + data + "");
                    }
                },
                error: function () {
                    alert('上传失败');
                }


            })
        }
    });

    $('#draft').click(function () {
        var poster = $("#activity_poster")[0].src;
        var _title = $('#title').val();
        var city = $('#city').val();
        var address = $('#address').val();
        var start_time = $('#start_date').val()+" "+$('#start_hour').val();
        var over_time = $('#over_date').val()+" "+$('#over_hour').val();
        var deadline = $('#deadline').val()+" "+$('#deadHour').val();
        var sponsor = $('#sponsor').val();
        var detail = getContent();
        var category = $('#project option:selected').val();
        var is_show = '0';
        var is_stick = '0';
        if($('#show').is(':checked')){
            is_show = '1'
        }
        if($('#stick').is(':checked')){
            is_stick = '1'
        }
        var ticket = $('#ticketType').val();
        var status = "1";//进行中
        var price = $('#price').val();
        var num = $('#num').val();
        //时间字符串的转换为日期  做判断
        var sta_str = start_time.replace(/-/g,"/");
        var dead_str = deadline.replace(/-/g,"/");
        var sta_date = new Date(sta_str);
        var dead_date = new Date(dead_str);
        var n = (sta_date-dead_date)/(1000*3600*24);
        if(_title == "" ||_title.length >= 20){
            alert('活动标题不能为空且要在20字以内');
            return;
        }
        if(city == "" ||address == ''){
            alert('请填写城市和地址');
            return;
        }
        //判断活动时间  结束时间 和报名截止时间的关系
        if(start_time>=over_time){
            alert('活动开始时间要小于结束时间');
            return;
        }
        if(start_time=='' || over_time ==''|| deadline == ''){
            alert('请选择日期');
            return;
        }
        if(n<1){
            alert('报名截止时间至少要在活动开始时间的前一天');
            return;
        }
        if(sponsor == ''){
            alert('请填写主办方');
            return;
        }
        if(detail == ''){
            alert('请填写活动详情');
            return;
        }
        if(price == "" ||!/^[0-9]*$/.test(price)){
            alert('请填写价格');
            return;
        }
        if(num == ""|| !/^[0-9]*$/.test(num)){
            alert('请填写数量');
            return;
        }


        $.ajax({
            type:'post',
            data:{
                "poster":poster,
                "title":_title,
                "city":city,
                "address":address,
                "start_time":start_time,
                "over_time":over_time,
                'deadline':deadline,
                "sponsor":sponsor,
                "detail":detail,
                "category":category,
                "is_show":is_show,
                "is_stick":is_stick,
                "ticket":ticket,
                "status":status,
                "price":price,
                "num":num
            },
            dataType: "json",
            url: "issue",
            success: function (data) {

                if(data=='0'){
                    alert('发布失败');
                }else{
                    alert('发布成功');
                    location.href='index?status=1';
                }

            }
        });
    });
    $('#publish').click(function () {
        //表单校验
        //发布存到数据库

        var poster = "haibao";
        var _title = $('#title').val();
        var city = $('#city').val();
        var address = $('#address').val();
        var start_time = $('#start_date').val()+" "+$('#start_hour').val();
        var over_time = $('#over_date').val()+" "+$('#over_hour').val();
        var deadline = $('#deadline').val()+" "+$('#deadHour').val();
        var sponsor = $('#sponsor').val();
        var detail = getContent();
        var category = $('#project option:selected').val();
        var is_show = '0';
        var is_stick = '0';
        if($('#show').is(':checked')){
            is_show = '1'
        }
        if($('#stick').is(':checked')){
            is_stick = '1'
        }
        var ticket = $('#ticketType').val();
        var status = "2";//进行中
        var price = $('#price').val();
        var num = $('#num').val();
        //时间字符串的转换为日期  做判断
        var sta_str = start_time.replace(/-/g,"/");
        var dead_str = deadline.replace(/-/g,"/");
        var sta_date = new Date(sta_str);
        var dead_date = new Date(dead_str);
        var n = (sta_date-dead_date)/(1000*3600*24);
        if(_title == "" ||_title.length >= 20){
            alert('活动标题不能为空且要在20字以内');
            return;
        }
        if(city == "" ||address == ''){
            alert('请填写城市和地址');
            return;
        }
        //判断活动时间  结束时间 和报名截止时间的关系
        if(start_time>=over_time){
            alert('活动开始时间要小于结束时间');
            return;
        }
        if(start_time=='' || over_time ==''|| deadline == ''){
            alert('请选择日期');
            return;
        }
        if(n<1){
            alert('报名截止时间至少要在活动开始时间的前一天');
            return;
        }
        if(sponsor == ''){
            alert('请填写主办方');
            return;
        }
        if(detail == ''){
            alert('请填写活动详情');
            return;
        }
        if(price == "" ||!/^[0-9]*$/.test(price)){
            alert('请填写价格');
            return;
        }
        if(num == ""|| !/^[0-9]*$/.test(num)){
            alert('请填写数量');
            return;
        }


        $.ajax({
            type:'post',
            data:{
                "poster":poster,
                "title":_title,
                "city":city,
                "address":address,
                "start_time":start_time,
                "over_time":over_time,
                'deadline':deadline,
                "sponsor":sponsor,
                "detail":detail,
                "category":category,
                "is_show":is_show,
                "is_stick":is_stick,
                "ticket":ticket,
                "status":status,
                "price":price,
                "num":num
            },
            dataType: "json",
            url: "issue",
            success: function (data) {

                if(data=='0'){
                    alert('发布失败');
                }else{
                    alert('发布成功');
                    location.href='index?status=2';
                }

            }
        });

    });


</script>



